from .base import BaseModel

class IPAddress(BaseModel):
    """IP地址模型"""
    
    def __init__(self):
        super().__init__()
        self.ip_id = None  # IP地址ID
        self.ip = None  # IP地址
        self.owner_id = None  # 所属单位ID
        self.owner_name = None  # 所属单位名称
        self.create_time = None  # 创建时间
        self.update_time = None  # 更新时间
    
    def __str__(self):
        return f"IPAddress(id={self.ip_id}, ip={self.ip}, owner={self.owner_name})"
    
    def to_dict(self):
        """将模型转换为字典格式"""
        return {
            'ip_id': self.ip_id,
            'ip': self.ip,
            'owner_id': self.owner_id,
            'owner_name': self.owner_name,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None
        }
    
    def is_valid(self):
        """验证IP地址格式是否有效"""
        # 简化版：检查是否包含三个点且由数字组成
        if not self.ip:
            return False
        
        parts = self.ip.split('.')
        if len(parts) != 4:
            return False
        
        for part in parts:
            if not part.isdigit():
                return False
            num = int(part)
            if num < 0 or num > 255:
                return False
        
        return True